/*
 *  app.cpp
 *  QuickMatch
 *
 *  Created by Leroy Ketelaars on 8/13/09.
 *  Copyright 2009 Zombies on Fire. All rights reserved.
 *
 */

#include "app.h"
#include "SDL.h"
#include "QMText.h"
#include "HitTest.h"
#include <iostream>
#include <exception>
#include "DebugFuncs.h"

using namespace std;

void app::go()
{
	delegate = this;
	run = true;
	SDL_Surface* bg = loadSprite("QuickMatch.app/Contents/Resources/bg.png");
	QMMatch* match = new QMMatch();
	this->forkTo(match);
	match->setBounds(0, 0, 200, 200);

	
	//set up the match widget decorations (borders)
	match->widget.corners[TOPLEFTCORNER] = loadSprite("QuickMatch.app/Contents/Resources/topleft.png");
	match->widget.corners[TOPRIGHTCORNER] = loadSprite("QuickMatch.app/Contents/Resources/topright.png");
	match->widget.corners[BOTTOMLEFTCORNER] = loadSprite("QuickMatch.app/Contents/Resources/bottomleft.png");
	match->widget.corners[BOTTOMRIGHTCORNER] = loadSprite("QuickMatch.app/Contents/Resources/bottomright.png");
	match->widget.edges[TOPEDGE] = loadSprite("QuickMatch.app/Contents/Resources/topline.png");
	match->widget.edges[LEFTEDGE] = loadSprite("QuickMatch.app/Contents/Resources/leftline.png");
	match->widget.edges[BOTTOMEDGE] = loadSprite("QuickMatch.app/Contents/Resources/bottomline.png");
	match->widget.edges[RIGHTEDGE] = loadSprite("QuickMatch.app/Contents/Resources/rightline.png");
	match->widget.fill = CreateSurface(match->widget.bounds.w - (match->widget.corners[TOPRIGHTCORNER]->w << 1), 
									match->widget.bounds.h - (match->widget.corners[BOTTOMRIGHTCORNER]->h << 1), 226, 226, 226);
	match->vs_seperator = loadSprite("QuickMatch.app/Contents/Resources/vsseperator.png");
	matches.push_back(match);
	//add players (in scope so player is destroyed once we are done with it)
	{
		QMPlayerContainer* player = new QMPlayerContainer();//, *p;
		player->name.font = TTF_OpenFont("QuickMatch.app/Contents/Resources/Essai.ttf", 11);
		player->name.setText("Contestant A");
		player->name.update();
		player->containerbg = loadSprite("QuickMatch.app/Contents/Resources/containerbg.png");
		match->players.push_back(*player);
	};
	
	//what is being executed below, is acctually dependent on which object the delegate pointer is pointing to
	//this could be the app, or any subclassed instance of QMView
	while(run)
	{
		SDL_BlitSurface(bg, NULL, scrn, NULL);
		run = delegate->handleInteraction();
		drawMatch(match);
		DrawCollisionrect(&(delegate->bounds), this->scrn);
		
		SDL_Flip(scrn);
		SDL_Delay(300);
	}
	SDL_FreeSurface(bg);
	match->players.clear();
}
void app::drawMatch(QMMatch* match)
{
	SDL_Rect rec = { 0, 0, 0, 0 };
	
	int rightstop = match->widget.bounds.x + match->widget.bounds.w - match->widget.corners[TOPRIGHTCORNER]->w;
	int bottstop = match->widget.bounds.y + match->widget.bounds.h - match->widget.corners[BOTTOMRIGHTCORNER]->h;

	rec.x = match->widget.bounds.x + match->widget.corners[TOPLEFTCORNER]->w;
	rec.y = match->widget.bounds.y + match->widget.corners[BOTTOMLEFTCORNER]->h;
	
	//draw the background fill colour first
	SDL_BlitSurface(match->widget.fill, NULL, scrn, &rec);
	rec.x = rec.y = 0;

	for(int y = match->widget.bounds.y + match->widget.corners[BOTTOMRIGHTCORNER]->h; y < bottstop; y+= match->widget.edges[LEFTEDGE]->h)
	{
		rec.x = match->widget.bounds.x;
		rec.y = y;
		SDL_BlitSurface(match->widget.edges[LEFTEDGE], NULL, scrn, &rec);
		rec.x = match->widget.bounds.x + match->widget.bounds.w - match->widget.corners[RIGHTEDGE]->w;
		SDL_BlitSurface(match->widget.edges[RIGHTEDGE], NULL, scrn, &rec);
	}
	for(int x = match->widget.bounds.x + match->widget.corners[TOPRIGHTCORNER]->w; x < rightstop; x+= match->widget.edges[BOTTOMEDGE]->w)
	{
		rec.y = match->widget.bounds.y;
		rec.x = x;
		SDL_BlitSurface(match->widget.edges[TOPEDGE], NULL, scrn, &rec);
		rec.y = match->widget.bounds.y + match->widget.bounds.h - match->widget.corners[RIGHTEDGE]->h;
		SDL_BlitSurface(match->widget.edges[BOTTOMEDGE], NULL, scrn, &rec);
	} 
	rec.x = match->widget.bounds.x;
	rec.y = match->widget.bounds.y;
	SDL_BlitSurface(match->widget.corners[TOPLEFTCORNER], NULL, scrn, &rec);
	rec.x = rightstop;
	SDL_BlitSurface(match->widget.corners[TOPRIGHTCORNER], NULL, scrn, &rec);
	rec.x = match->widget.bounds.x;
	rec.y = bottstop;
	SDL_BlitSurface(match->widget.corners[BOTTOMLEFTCORNER], NULL, scrn, &rec);
	rec.x = rightstop;
	SDL_BlitSurface(match->widget.corners[BOTTOMRIGHTCORNER], NULL, scrn, &rec);
	
	rec.x = match->widget.bounds.x + (match->widget.bounds.w >> 1) - ( match->vs_seperator->w >> 1);
	rec.y = match->widget.bounds.y + (match->widget.bounds.h >> 1) - ( match->vs_seperator->h >> 1);
	SDL_BlitSurface(match->vs_seperator, NULL, scrn, &rec);
	
	//loop through the vector, drawing each player
	
	int po = 50;
	for(match->players_iter = match->players.begin(); match->players_iter != match->players.end(); match->players_iter++)
	{
		rec.y = match->widget.bounds.y + po; 
		rec.x = match->widget.bounds.x + match->widget.corners[TOPLEFTCORNER]->w + 14;
		po+=50;
		QMPlayerContainer& player_t = *match->players_iter;
		SDL_BlitSurface(player_t.name.render, NULL, scrn, &rec);
	}
	//DrawCollisionrect(&(match->widget.bounds), this->scrn);

	
	/*
	for(int x = 0; x < rightstop; x+= match->widget->corners[TOPRIGHTCORNER]->h)
	{
		rec.x = x;
		SDL_BlitSurface(edges[TOPEDGE], NULL, srcn, &rec);
	}
	
	match->widget*/
}
void app::drawPanel(QMPanel* panel)
{
	if(panel)
	{
		if(panel->visible)
		{
			if(!panel->is_collapsed)
			{
				if(panel->is_animating)
				{
					if(panel->temprect.x > panel->bounds.x) panel->temprect.x--;
					if(panel->render)
					{
						SDL_BlitSurface(panel->render, NULL, this->scrn, &panel->temprect);
					}
					else printf("can't draw panel animation @%p without graphics\n", this);
				}
				else
				{
					if(!panel->render)
					{
						printf("creating render\n");
						if(panel->fill)
						{
							printf("fill image is OK, moving on\n");
							panel->render = CreateSurface(panel->bounds.w, scrn->h, 0xff, 0, 0xff);
							SDL_Rect r = {0, 0, 0, 0};
							for(int i = 0; i < scrn->h + 1; i+=panel->fill->h)
							{
								SDL_BlitSurface(panel->fill, NULL, panel->render, &r);
								r.y = i;
							}	
						}
						printf("Done\n");
					}
					if(panel->render)
					{
						SDL_BlitSurface(panel->render, NULL, this->scrn, &panel->bounds);
						
					}
					else printf("can't draw panel @%p without graphics\n", this);				
				}
			}
			
			//drawing
			
		}
	}
}

bool app::handleInteraction()
{
	while ( SDL_PollEvent(events) ) 
	{
		switch (events->type)
		{
			case SDL_QUIT:
				printf("Recieved SDL_Quit Event in app\n");
				return false;
				break;
			case SDL_MOUSEMOTION:
			{
				mx = events->motion.x;
				my = events->motion.y;
			}
			break;	
			case SDL_MOUSEBUTTONUP:
				if(events->button.button == SDL_BUTTON_LEFT)
				{
					if(events->button.state == SDL_RELEASED)
					{
						bool nohits = true;
						for(matchiterator = matches.begin(); matchiterator != matches.end(); matchiterator++)
						{
							//printf(" > Looking for QMMatch object to hand-off input handling to\n");
							QMMatch* match_t = *matchiterator;
							if(HitTest(this->mx, this->my, match_t->widget.bounds))
							{
								delegate = match_t;
								nohits = false;
								//printf(" -> delegated input handling to QMMatch @ %p\n", delegate);
								return true;
							}

						} 
						if(nohits) delegate = this;
					}
				}
			break;
			default: break;
		}
	}
	return true;
}


SDL_Surface* CreateSurface(int w, int h, unsigned char r, unsigned char g, unsigned char b)
{
	Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
	rmask = 0xff000000;
	gmask = 0x00ff0000;
	bmask = 0x0000ff00;
	amask = 0x000000ff;
#else
	rmask = 0x000000ff;
	gmask = 0x0000ff00;
	bmask = 0x00ff0000;
	amask = 0xff000000;
#endif
	SDL_Surface* scrn = SDL_GetVideoSurface();
	SDL_Rect rec = { 0, 0, w, h };
	Uint32 color = SDL_MapRGB(scrn->format, r, g, b);
	
	SDL_Surface* surf_t = NULL;
	surf_t = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, scrn->format->BitsPerPixel, 
											   rmask, gmask, bmask, amask);
	if(surf_t != NULL)
	{
		SDL_FillRect(surf_t, &rec, color);
		SDL_Surface* returnsurface = NULL;
		returnsurface = SDL_DisplayFormat(surf_t);
		SDL_FreeSurface(surf_t);
		return returnsurface;
	}
	return NULL;
}
